package com.xh.one2one.knowledgeSystem.service.impl;

import com.xh.one2one.knowledgeSystem.bean.request.KnowledgeManagerElementRelationRequest;
import com.xh.one2one.knowledgeSystem.model.KnowledgeManagerElementRelation;
import com.xh.one2one.knowledgeSystem.dao.KnowledgeManagerElementRelationMapper;
import com.xh.one2one.knowledgeSystem.service.IKnowledgeManagerElementRelationService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
 * <p>
 * 知识元管理与知识元关联关系表 服务实现类
 * </p>
 *
 * @author TW
 * @since 2021-04-27
 */
@Service
public class KnowledgeManagerElementRelationServiceImpl extends BaseServiceImpl<KnowledgeManagerElementRelationMapper, KnowledgeManagerElementRelation> implements IKnowledgeManagerElementRelationService {
    @Override
    public XhPage queryKnowledgeManagerElementRelationPage(XhPage page, KnowledgeManagerElementRelationRequest bean) {
        QueryWrapper qw=new QueryWrapper<KnowledgeManagerElementRelation>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryKnowledgeManagerElementRelation(KnowledgeManagerElementRelationRequest bean) {
        QueryWrapper qw=new QueryWrapper<KnowledgeManagerElementRelation>();
        //拼装条件
        qw=prepareParams(bean, qw);
        if(!StringUtils.isEmpty(bean.getManageId())){
            qw.eq("manage_id",bean.getManageId());
        }
        return this.dao.selectList(qw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteByKnowledgeId(String[] ids) {
        QueryWrapper qw=new QueryWrapper<KnowledgeManagerElementRelation>();
        qw.in("knowledge_id",ids);
        this.dao.delete(qw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSave(KnowledgeManagerElementRelationRequest request) {
        //先删除
        this.dao.deleteByManageId(request.getManageId());
        //再添加
        List<KnowledgeManagerElementRelation> list = new ArrayList<>();
        for (int i = 0; i < request.getKnowledgeIds().size(); i++) {
            String knowledgeId = request.getKnowledgeIds().get(i);
            KnowledgeManagerElementRelation managerElementRelation = new KnowledgeManagerElementRelation();
            managerElementRelation.setManageId(request.getManageId());
            managerElementRelation.setKnowledgeId(knowledgeId);
            list.add(managerElementRelation);
        }
        if(list.size()>0){
            this.batchInsert(list);
        }
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(KnowledgeManagerElementRelationRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }
}
